package com.example.Project.user.Methods;

import javafx.scene.control.TextInputDialog;
import javafx.stage.DirectoryChooser;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

public class ToExcel {
    public static <T> void exportToExcel(List<T> dataList, String filePath, String sheetName) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet(sheetName);

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        int cellNum = 0;
        Class<?> clazz = dataList.get(0).getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            headerRow.createCell(cellNum++).setCellValue(fieldName);
        }

        // 创建数据行
        int rowNum = 1;
        for (T data : dataList) {
            Row row = sheet.createRow(rowNum++);
            int cellIndex = 0;

            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    Object value = field.get(data);
                    row.createCell(cellIndex++).setCellValue(value != null ? value.toString() : "");
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        // 将Workbook写入文件
        try (FileOutputStream fileOut = new FileOutputStream(filePath+"\\"+sheetName)) {
            workbook.write(fileOut);
        }

        // 关闭Workbook，释放资源
        workbook.close();
    }
    public static <T> void setInfo(List<T> Info){
        try {
            System.out.println(Info.get(0));
            // 创建文件名输入对话框
            TextInputDialog fileNameDialog = new TextInputDialog();
            fileNameDialog.setTitle("文件名输入");
            fileNameDialog.setHeaderText("请输入文件名:");
            fileNameDialog.setContentText("文件名:");

            // 显示文件名输入对话框并等待用户输入
            java.util.Optional<String> fileNameResult = fileNameDialog.showAndWait();

            // 处理文件名输入
            fileNameResult.ifPresent(fileName -> {
                System.out.println("文件名: " + fileName);

                // 创建文件路径选择对话框
                DirectoryChooser directoryChooser = new DirectoryChooser();
                directoryChooser.setTitle("选择文件路径");

                // 显示文件路径选择对话框并等待用户选择
                File selectedDirectory = directoryChooser.showDialog(null);

                // 处理文件路径选择
                if (selectedDirectory != null) {
                    System.out.println("选择的文件路径: " + selectedDirectory.getAbsolutePath());

                    // 继续导出到Excel或进行其他处理
                    try {
                        // 导出Excel文件
                        exportToExcel(Info,  selectedDirectory.getAbsolutePath(),fileName + ".xlsx");
                        System.out.println("Excel导出成功！");
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
